package com.web.controller;

import com.web.entity.HealthInfo;
import com.web.entity.PageResult;
import com.web.entity.Result;
import com.web.service.impl.HealthInfoServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/healthInfos")
@CrossOrigin
public class HealthInfoController {

    @Autowired
    private HealthInfoServiceImpl healthInfoService;

    //http://localhost:8080/healthInfos/listByUserIdAndDate?userId=2&recordDate=2025-09-19
    @GetMapping("/listByUserIdAndDate")
    public Result listByUserIdAndDate(HealthInfo healthInfo){
        log.info("查询结果：{}", healthInfo);
        List<HealthInfo> list = healthInfoService.listByUserIdAndDate(healthInfo);
        return Result.success(list);
    }

    //http://localhost:8080/healthInfos/getByUserIdAndDate?userId=1&recordDate=2020-05-01
    // 查询
    @GetMapping("/getByUserIdAndDate")
    public Result getByUserIdAndDate(HealthInfo healthInfo){
        log.info("查询结果：{}", healthInfo);
        HealthInfo byUserIdAndDate = healthInfoService.getByUserIdAndDate(healthInfo);
        return Result.success(byUserIdAndDate);
    }

    /*
    分页查询
     */
    @GetMapping
    public Result page(Integer page, Integer pageSize, HealthInfo healthInfo){
        log.info("分页查询:{}",healthInfo);
        PageResult<HealthInfo> pageResult = healthInfoService.page(page,pageSize,healthInfo);
        return Result.success(pageResult);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("id查询:{}",id);
        HealthInfo healthInfo = healthInfoService.findById(id);
        return Result.success(healthInfo);
    }
    // http://localhost:8080/healthInfos/findByUserId?userId=1
    @GetMapping("/findByUserId")
    public Result findByUserId(Integer userId){
        log.info("查询结果：{}", userId);
        List<HealthInfo> list = healthInfoService.findByUser(userId);
        return Result.success(list);
    }

    @PutMapping("/update")
    public Result update(@RequestBody HealthInfo healthInfo){
        log.info("修改数据:{}",healthInfo);
        healthInfoService.update(healthInfo);
        return Result.success();
    }


    @PostMapping("/insert")
    public Result insert(@RequestBody HealthInfo healthInfo){
        log.info("新增记录:{}",healthInfo);
        healthInfoService.insert(healthInfo);
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除记录:{}",id);
        healthInfoService.delete(id);
        return Result.success();
    }
}
